﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SEO.Entity;
using System.Data;
using System.Data.SqlClient;

namespace SEO.Business
{
    /// <summary>
    /// 抓虫运行日志
    /// </summary>
    public class BisSysRunLog
    {
        /// <summary>
        /// 新增运行日志
        /// </summary>
        /// <param name="Info"></param>
        /// <returns></returns>
        public int Insert(SysRunLog Info)
        {
            string strSql = "insert into [SysRunLog]([BeginTime],[RunIndex]) values(@BeginTime,@RunIndex)";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@BeginTime", Info.BeginTime));
            parms.Add(new SqlParameter("@RunIndex", Info.RunIndex));
            return SqlHelper.ExecuteQuery(SqlHelper.database, CommandType.Text, strSql, parms.ToArray());
        }

        /// <summary>
        /// 更新运行日志
        /// </summary>
        /// <param name="Info"></param>
        /// <returns></returns>
        public bool Update(SysRunLog Info)
        {
            string strSql = "update [SysRunLog] set [EndTime]=@EndTime where [ID]=@ID";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@EndTime", Info.EndTime));
            parms.Add(new SqlParameter("@ID", Info.ID));
            return SqlHelper.ExecuteNonQuery(SqlHelper.database, CommandType.Text, strSql, parms.ToArray()) > 0;
        }

        /// <summary>
        /// 获取最近一条运行记录
        /// </summary>
        /// <returns></returns>
        public DataTable GetLastLog(DateTime dBeginTime, DateTime dEndTime)
        {
            return SqlHelper.ExecuteDataset(
                   SqlHelper.database,
                   CommandType.Text,
                   "select top 1 * from [SysRunLog] where [BeginTime] between @BeginTime and @EndTime and [EndTime] is not null order by [ID] desc",
                   new SqlParameter("@BeginTime", dBeginTime),
                   new SqlParameter("@EndTime", dEndTime)).Tables[0];
        }

        /// <summary>
        /// 获取最近一条运行记录
        /// </summary>
        /// <returns></returns>
        public DataTable GetSysLastLog(DateTime dBeginTime, DateTime dEndTime)
        {
            return SqlHelper.ExecuteDataset(
                   SqlHelper.database,
                   CommandType.Text,
                   "select top 1 * from [SysRunLog] where [BeginTime] between @BeginTime and @EndTime order by [ID] desc",
                   new SqlParameter("@BeginTime", dBeginTime),
                   new SqlParameter("@EndTime", dEndTime)).Tables[0];
        }

        /// <summary>
        /// 获取最近一条运行记录
        /// </summary>
        /// <returns></returns>
        public DataTable GetLastLog(int nRunIndex, DateTime dBeginTime, DateTime dEndTime)
        {
            return SqlHelper.ExecuteDataset(
                   SqlHelper.database,
                   CommandType.Text,
                   "select top 1 * from [SysRunLog] where [BeginTime] between @BeginTime and @EndTime and [RunIndex]=@RunIndex",
                   new SqlParameter("@BeginTime", dBeginTime),
                   new SqlParameter("@EndTime", dEndTime),
                   new SqlParameter("@RunIndex", nRunIndex)).Tables[0];
        }

        /// <summary>
        /// 获取当天次数
        /// </summary>
        /// <param name="dBeginTime"></param>
        /// <param name="dEndTime"></param>
        /// <returns></returns>
        public DataTable GetAllList(DateTime dBeginTime, DateTime dEndTime)
        {
            return SqlHelper.ExecuteDataset(
                  SqlHelper.database,
                  CommandType.Text,
                  "select [RunIndex] from [SysRunLog] where [BeginTime] between @BeginTime and @EndTime order by [RunIndex]",
                  new SqlParameter("@BeginTime", dBeginTime),
                  new SqlParameter("@EndTime", dEndTime)).Tables[0];
        }

        /// <summary>
        /// 获取运行日志
        /// </summary>
        /// <returns></returns>
        public DataTable GetList(DateTime dBeginTime, DateTime dEndTime, int nPageIndex, int nPageSize, ref int nPageCount, ref int nRecordCount)
        {
            SqlParameter[] parms = new SqlParameter[6];
            parms[0] = new SqlParameter("@BeginTime", dBeginTime);
            parms[1] = new SqlParameter("@EndTime", dEndTime);
            parms[2] = new SqlParameter("@PageIndex", nPageIndex);
            parms[3] = new SqlParameter("@PageSize", nPageSize);

            //返回值
            parms[4] = new SqlParameter("@PageCount", nPageCount);
            parms[4].Direction = ParameterDirection.Output;
            parms[5] = new SqlParameter("@RecordCount", nRecordCount);
            parms[5].Direction = ParameterDirection.Output;

            DataTable dt = SqlHelper.ExecuteDataset(
                SqlHelper.database,
                CommandType.StoredProcedure,
                "GetSysLogList",
                parms).Tables[0];
            nPageCount = Convert.ToInt32(parms[4].Value);
            nRecordCount = Convert.ToInt32(parms[5].Value);
            return dt;
        }
    }
}
